package co.chuangdian.oflauncher.widgets.system;

import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import co.chuangdian.oflauncher.R;
import co.chuangdian.oflauncher.setting.FeaturesActivity;
import uc.IconView;

public class SettingWidget extends LinearLayout {
	private Activity context;
	private GridView setting_grid;
	private String[] settingName;
	private TypedArray settingIcon;
	private List<Setting> list = new ArrayList<Setting>();
	public int height;

	public SettingWidget(Context context) {
		super(context);
		this.context = (Activity) context;
		init();
	}

	@SuppressLint("Recycle")
	private void init() {
		view = View.inflate(context, R.layout.widget_setting, this);
		setting_grid = (GridView) view.findViewById(R.id.setting_grid);
		adapter = new SettingAdapter();
		setting_grid.setAdapter(adapter);
		// setting_grid.setOnChangeListener(chanageListener);

		settingName = getResources().getStringArray(R.array.setting_name);
		settingIcon = getResources().obtainTypedArray(R.array.setting_icon);
		for (int i = 0; i < settingName.length; i++) {
			Setting set = new Setting();
			set.setSetName(settingName[i]);
			set.setSetIcon(settingIcon.getResourceId(i, 0));
			list.add(set);
		}
		setting_grid.setOnItemClickListener(itemClickListener);
	}

	private OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			switch (position) {
			// 主题
			case 0:
				context.startActivity(new Intent(context, FeaturesActivity.class));
				break;
			// 壁纸
			case 1:
				context.startActivity(
						new Intent(context, co.chuangdian.oflauncher.wallpapers.setting.HomeActivity.class));
				break;
			// 演示
			// launcher设置
			case 2:
				context.startActivity(new Intent(context, co.chuangdian.oflauncher.setting.HomeActivity.class));
				break;
			// 系统设置
			case 3:
				Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
				settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				context.startActivity(new Intent(settings));
				break;
			default:
				break;

			}
		}
	};
	public boolean bool = false;
	private SettingAdapter adapter;
	private View view;

	private class SettingAdapter extends BaseAdapter  {

		@Override
		public int getCount() {

			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();

				convertView = new IconView(context);
				holder.setIcon = (IconView) convertView;
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			@SuppressWarnings("deprecation")
			Drawable icon = context.getResources().getDrawable(list.get(position).getSetIcon());
			holder.setIcon.setData(icon, list.get(position).getSetName());
			holder.setIcon.hideTitle(bool);
			return convertView;
		}

		private class ViewHolder {
			IconView setIcon;
		}

	
	}

	// 设置标题的隐藏
	public void setVisible(boolean bool) {
		this.bool = bool;
		adapter.notifyDataSetChanged();

	}
}